/**
 * NavUri 0.1.1
 *
 * https://code.google.com/js-navuri
 *
 * Copyright (c) 2011 Intelygenz <www.intelygenz.com>
 * All rights reserved.
 * 
 * BSD License
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Creation date: 2011-08-18
 * 
 * order 
 * [version] date (author)
 * 		upd|fix|new|dep - description
 *
 * [0.1.1] 2011-08-19 (angel.sanchez@intelygenz.com, jose.perez@intelygenz.com, jonas.dacruz@intelygenz.com) 
 * 			new - 
 * 
 */

NavUri = new function (){

	var _Debugging = true;

	function _Debug ( p_obj1, p_obj2, p_obj3, p_obj4, p_obj5 ){
		if (_Debugging && window.console && window.console.log){
			window.console.log("[NavUri]", p_obj1, p_obj2, p_obj3, p_obj4, p_obj5);
		}
	};
	
	function _Error ( p_obj1, p_obj2, p_obj3, p_obj4, p_obj5 ){
		if (window.console && window.console.log){
			window.console.error("[NavUri]", p_obj1, p_obj2, p_obj3, p_obj4, p_obj5);
		}
	};

	var _History=["", ""];
	var _Events=[];
	
	/*
	 * 
	 * */
	function _IsLevelEmpty(p_depth){
		//alert( document.location.hash.split("/").length );
		return document.location.hash.split("/").length<=p_depth;
	}

	function _GetLabel(p_hashPart){
		 return p_hashPart.split("?")[0].replace("#", "");
	}
	
	function _GetParams(p_hashPart){
		 var paramsTmp = p_hashPart.split("?")
		 	, params = {}
		 	, param
		 ;

		 if ( paramsTmp.length>1 ){
			 paramsTmp = paramsTmp[1].split("&");
			 for ( var f=0, F=paramsTmp.length; f<F; f++ ){
				 param = paramsTmp[f].split("=");
				 params[ param[0] ] = param[1];
			 }
		 }

		 return params;
	}
	
	function _OnHashChange(){

		if ( _History[_History.length-1]!=document.location.hash ){
			_Debug("<-", document.location.hash);
			_Debug( "_History[pre]", _History , _History.length)
			if ( _History.length>2 ){
				_History.pop();
			}
			_Debug( "_History[post]", _History, _History.length )
			_Navigate(document.location.hash, true);
		}
		
		var prev = _History[_History.length-2].split("/");
		var curr = _History[_History.length-1].split("/");

_Debug("************_OnHashChange**************", document.location.hash, prev, curr);
		
		for ( var f=0, F=curr.length; f<F; f++ ){
			if ( prev[f]!=curr[f] ){
				for ( var g=0, G=_Events[f].length; g<G; g++){
					_Debug("************_OnHashChange LAUNCH **************",  _GetLabel(curr[f]), _GetParams(curr[f]) );
					_Events[f][g]( _GetLabel(curr[f]), _GetParams(curr[f]) );
				}				
			}
		}
		
	}

	function _LastTimeInHistory(p_hashUri){
		var lastHashUri = p_hashUri;
		for ( var f=_History.length-1, F=0; f>=F; f-- ){
			if ( _History[f].indexOf( p_hashUri ) == 0 ){
				lastHashUri = _History[f];
				break;
			}
		}
		return lastHashUri;
	}

	function _Navigate(p_hashUri, p_noSave){
		var hashUri = _LastTimeInHistory( p_hashUri );
		if(p_noSave) _History[_History.length] = hashUri;
		document.location.hash = hashUri;
_Debug( "_Navigate", p_hashUri, _History );
	}

	function _Register(p_depth, f_call){
		if( !_Events[p_depth] ){
			_Events[p_depth] = [];
		}
		var len = _Events[p_depth].length;
		_Events[p_depth][len] = f_call;
	}

	// public
	this.Navigate = _Navigate;
	this.Register = _Register;
	this.IsLevelEmpty = _IsLevelEmpty;
	this.Init = function (p_initHashUri){
		var hashUri = document.location.hash;
		if ( !hashUri ){
			hashUri = p_initHashUri; 
		}
		if(!hashUri){
			_Error("No default hashUri");
		}
		_Navigate(hashUri);
		_OnHashChange();
	}

	$(window).bind("hashchange", _OnHashChange);
}
